//
//  ZYSRecommendTableVC.m
//  budejie
//
//  Created by oldSix_Zhu on 16/8/27.
//  Copyright © 2016年 oldSix_Zhu. All rights reserved.
//

#import "ZYSRecommendTableVC.h"
#import "ZYSTableVCHeader.h"
#import "ZYSImgHeaderModels.h"
#import "ZYSBtnHeaderModels.h"
#import "ZYSCellModels.h"
#import "ZYSTableViewCell.h"
#import "ZYSTagTableVC.h"

@interface ZYSRecommendTableVC ()
<
ZYSTableVCHeaderDelegate
>

@property (nonatomic,strong)NSArray *cellArray;

//十字按钮
@property (nonatomic,weak)UIButton *addBtn;

//cell高
//@property (nonatomic,assign)CGFloat cellHeight;

@end

@implementation ZYSRecommendTableVC

-(void)setTheme_id:(NSInteger)theme_id
{
    _theme_id = theme_id;
    
    NSString *urlStr = [NSString stringWithFormat:@"http://s.budejie.com/topic/tag-topic/%ld/new/bs0315-iphone-4.3/0-20.json",self.theme_id];
    
    [ZYSCellModels loadCellWithUrlStr:urlStr successBlock:^(NSArray *array) {
       
        self.cellArray = array;
        
        [self.tableView reloadData];
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

//    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self createAddBtn];
    
    [self createButton];
    
    //预估行高
    self.tableView.estimatedRowHeight = 200;
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self createHeader];
}
//创建navbarButton
-(void)createButton
{
    //右边
    UIButton *btnR = [[UIButton alloc]init];
    
    [btnR setBackgroundImage:[UIImage imageNamed:@"tag_subscribe_tag_bg_52x28_"] forState:UIControlStateNormal];
    [btnR setBackgroundImage:[UIImage imageNamed:@"tag_subscribe_tag_bgN_52x28_"] forState:UIControlStateHighlighted];
    
    [btnR setTitle:@"＋ 订阅" forState:UIControlStateNormal];
    [btnR setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    btnR.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [btnR sizeToFit];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnR];
    
    //左边
    UIButton *btnL = [[UIButton alloc]init];
    
    [btnL setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
    [btnL setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
    
    [btnL setTitle:@"返回" forState:UIControlStateNormal];
    [btnL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnL setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    [btnL sizeToFit];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btnL];
    
    //点击事件
    [btnL addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

//十字按钮
-(void)createAddBtn
{
    UIButton *add = [[UIButton alloc]init];
    
    [add setImage:[UIImage imageNamed:@"tag_publish_post_36x36_"] forState:UIControlStateNormal];
    
    [add sizeToFit];
    
    CGFloat addW = add.frame.size.width;
    CGFloat addH = add.frame.size.height;
    CGFloat addX = ScreenW - addW - 12;
    CGFloat addY = ScreenH - addH - 12 ;
    
    add.frame = CGRectMake(addX, addY, addW, addH);
    
    self.addBtn = add;
    
    [self.navigationController.view  addSubview:add];
    
    //添加事件
    [add addTarget:self action:@selector(didClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    self.addBtn.hidden = YES;
}

-(void)didClickAddBtn
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"发图片",nil];
    
    [sheet  showInView:self.view];
}


//添加头
-(void)createHeader
{
    ZYSTableVCHeader *header = [[[NSBundle mainBundle]loadNibNamed:@"ZYSTableVCHeader" owner:nil options:nil] lastObject];
    //设置代理
    header.delegate = self;

    
    self.tableView.tableHeaderView = header;   
    
    //图片头
    NSString *urlImgStr = [NSString stringWithFormat:@"api_open.php?a=theme_info&appname=bs0315&asid=B9E7881C-C4F2-4996-9850-521DC65DAC49&c=topic&client=iphone&device=ios%%20device&from=ios&jbk=0&mac=&market=&openudid=2c1d53b0c26caba11781db5f9c3ebbb22c2864c0&sex=m&theme_id=%ld&udid=&uid=19183779&ver=4.3",self.theme_id];
    
    [ZYSImgHeaderModels loadImgHeaderWithUrlStr:urlImgStr successBlock:^(ZYSImgHeaderModels *model) {
        
        self.navigationItem.title = model.theme_name;
        
        header.imgModel = model;
        //动态改变组头高
        if ([model.info isEqualToString:@""] || model.info.length <= 0)
        {
            header.viewHeight.constant = 0;
            header.h -= 60;
            
        }
        self.tableView.tableHeaderView = header;
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    //Button头
    NSString *urlBtnStr = [NSString stringWithFormat:@"api_open.php?a=theme_users&appname=bs0315&asid=B9E7881C-C4F2-4996-9850-521DC65DAC49&c=topic&client=iphone&device=ios%%20device&from=ios&jbk=0&mac=&market=&openudid=2c1d53b0c26caba11781db5f9c3ebbb22c2864c0&sex=m&theme_id=%ld&udid=&uid=19183779&ver=4.3",self.theme_id];
    
    [ZYSBtnHeaderModels loadBtnHeaderWithUrlStr:urlBtnStr successBlock:^(NSArray *array) {
       
        header.btnArray = array;
        
        
    } failBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
}

//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.cellArray.count;
}

//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zysTableViewCell"];
    
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ZYSTableViewCell" owner:nil options:nil]lastObject];
    }
    
    cell.model = self.cellArray[indexPath.row];
//    self.cellHeight = cell.cellHeight;
    return cell;
}

//行高
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    return self.cellHeight;
//}

#pragma mark - 代理方法
//跳转到标签贡献榜单界面
-(void)pushToBangDanTableVC:(ZYSTableVCHeader *)tableVCHeader
{
    self.hidesBottomBarWhenPushed = YES;
    
    ZYSTagTableVC *tagVC = [[ZYSTagTableVC alloc]init];
    
    tagVC.theme_id = self.theme_id;
    
    [self.navigationController pushViewController:tagVC animated:YES];
    
}

//更新界面
-(void)update:(ZYSTableVCHeader *)tableVCHeader
{
    [self.tableView reloadData];
}

@end
